<?php

namespace app\admin\model\prl;

use think\Model;
use traits\model\SoftDelete;

class Articles extends Model
{

    use SoftDelete;

    // 表名
    protected $table = 'prl_articles';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'releasetime_text',
        'status_text',
        'display_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }     

    public function getDisplayList()
    {
        return ['0' => __('Display 0'), '1' => __('Display 1')];
    }     


    public function getReleasetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['releasetime']) ? $data['releasetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDisplayTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['display']) ? $data['display'] : '');
        $list = $this->getDisplayList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setReleasetimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function articlesChannelsAccess()
    {
        return $this->belongsToMany('Channels', '\\app\\admin\\model\\prl\\Articleschannelsaccess', 'channels_id', 'articles_id');
    }
}
